// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// T [author] rhyme_lph
// i [email]  rhymelph@gmail.com
// B [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/mixin/format_group_map_mixin.dart';
import 'package:finance_app/src/mixin/search_data_list_mixin.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/all_reports/age/age_select_filter_page.dart';

class AgeSelectFilterProvider extends BaseProvider
    with FormatGroupMapLocalMixin<String>, SearchDataListMixin {
  List<String> filterList = [];

  String currentSelect;

  @override
  Future get dataFuture => null;

  void onReset() {
    obtainContext?.call((context) {
      Navigator.of(context).pop(false);
    });
  }

  void onSelect(String data) {
    currentSelect = data;
    update();
    obtainContext?.call((context) {
      Navigator.of(context).pop(currentSelect);
    });
  }

  void initList(List<String> dataList) {
    filterList = dataList;
    contactToMap(filterList);
  }

  @override
  void searchRefreshData(searchData) {
    contactToMap(filterList);
    update();
  }

  void contactToMap(List<String> list) {
    groupMap.clear();
    sortGroupToMap(list,
        sort: (a, b) => sortName(a, b, (t) => t),
        group: (a) => groupFirstName(a, (t) => t),
        filter: (a) => onFilterSearch(searchController.text, a));
  }
}
